﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.WebRequestMethods;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using File = System.IO.File;

namespace Tools
{
    public partial class fileSortRenameForm : UserControl
    {
        [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
        private static extern int StrCmpLogicalW(string psz1, string psz2);
        List<string> fileSort = new List<string> { "文件名升序", "文件名降序" };

        public fileSortRenameForm()
        {
            InitializeComponent();
            fileSortComboBox.DataSource = fileSort;
            Dictionary<String, String> keys = new Dictionary<String, String>();
            FileStream fileStream = new FileStream("fileSortRename.data", FileMode.OpenOrCreate);
            if (fileStream.Length > 0)
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();

                keys = binaryFormatter.Deserialize(fileStream) as Dictionary<String, String>;

                string sourceFilePathText = "";
                keys.TryGetValue("sourceFilePathText", out sourceFilePathText);
                sourceFilePathTextBox.Text = sourceFilePathText;

                string spitTextBoxText = "";
                keys.TryGetValue("spitTextBoxText", out spitTextBoxText);
                spitTextBox.Text = spitTextBoxText;

            }
            fileStream.Close();
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            if (spitTextBox.Text!=null && spitTextBox.Text.Length >0) {
                string spitStr = spitTextBox.Text;
                string result = "";
                if (targetFileNamesTextBox.Text != null && targetFileNamesTextBox.Text.Length > 0)
                {
                    string[] lines = targetFileNamesTextBox.Text.Split('\n');
                    foreach (var item in lines)
                    {
                        string[] keys = item.Replace("\r\n", "").Replace("\n", "").Replace("\r", "").Split(new string[] { spitStr }, StringSplitOptions.None);
                        foreach (var item1 in keys)
                        {
                            result = result + item1 + "\r\n";
                        }

                    }
                    if (result.EndsWith("\r\n")) {
                        result = result.Remove(result.Length - "\r\n".Length);
                    }
                    targetFileNamesTextBox.Text = result;
                }
            }

            Console.WriteLine("sourceFilePathTextBox.Text:" + sourceFilePathTextBox.Text);
            Console.WriteLine("sourceFileListShowTextBox.Text:" + sourceFileListShowTextBox.Text);
            Console.WriteLine("targetFileNamesTextBox.Text:" + targetFileNamesTextBox.Text);
            Console.WriteLine("开始处理.Text----------------------------------------------");

            if (targetFileNamesTextBox.Text != null && targetFileNamesTextBox.Text.Length > 0)
            {
                if (sourceFilePathTextBox.Text != null && sourceFilePathTextBox.Text.Length > 0)
                {
                    string[] targetFileNames = targetFileNamesTextBox.Text.Split('\n');
                    string[] sourceFileListShows = sourceFileListShowTextBox.Text.Split('\n');

                    if (targetFileNames.Length != sourceFileListShows.Length)
                    {
                        MessageBox.Show("文件数量异常,左边:"+ sourceFileListShows.Length+" 右边:" + targetFileNames.Length, "错误");
                    }
                    else {
                        for (int i = 0; i < targetFileNames.Length; i++) {
                            //原文件
                            string sourceFileAllPathName = sourceFilePathTextBox.Text + "\\" + sourceFileListShows[i].Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
                            string suffix = Path.GetExtension(sourceFileAllPathName);
                            //改名文件
                            string targetAllPathName = sourceFilePathTextBox.Text + "\\" + targetFileNames[i].Replace("\r\n", "").Replace("\n", "").Replace("\r", "") + suffix;
                            Console.WriteLine("原文件名:" + sourceFileAllPathName);
                            Console.WriteLine("新文件名:" + targetAllPathName);
                            if (File.Exists(sourceFileAllPathName))
                            {
                                Console.WriteLine("开始修改文件名---------------------------------------------");
                                File.Move(sourceFileAllPathName, targetAllPathName);
                                Console.WriteLine("改名成功");
                            }
                        }
                        MessageBox.Show("文件修改完成");
                    }
                }
                else
                {
                    MessageBox.Show("原文件路径不能为空", "错误");
                }
            }
            else
            {
                MessageBox.Show("新文件名称不能为空", "错误");
            }
        }

        private void showFileButton_Click(object sender, EventArgs e)
        {
            string path = @"" + sourceFilePathTextBox.Text;
            DirectoryInfo root = new DirectoryInfo(path);
            FileInfo[] files = root.GetFiles();
            if (fileSortComboBox.Text.Equals("文件名升序"))
            {
                Array.Sort(files, (x, y) => StrCmpLogicalW(x.Name, y.Name));
            }
            else if (fileSortComboBox.Text.Equals("文件名降序"))
            {
                Array.Sort(files, (x, y) => -StrCmpLogicalW(x.Name, y.Name));
            }
            string sourceFileListShowText = "";
            for (int i = 0; i < files.Length; i++)
            {
                if (i != files.Length - 1)
                {
                    sourceFileListShowText = sourceFileListShowText + files[i].Name + "\r\n";
                }
                else {
                    sourceFileListShowText = sourceFileListShowText + files[i].Name;
                }
            }
            sourceFileListShowTextBox.Text = sourceFileListShowText;
        }

        private void saveConfigButton_Click(object sender, EventArgs e)
        {
            Dictionary<String, String> keys = new Dictionary<String, String>();

            FileStream fileStream = new FileStream("fileSortRename.data", FileMode.Create);
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            keys.Add("sourceFilePathText", this.sourceFilePathTextBox.Text);
            keys.Add("spitTextBoxText", this.spitTextBox.Text);

            binaryFormatter.Serialize(fileStream, keys);

            fileStream.Close();
        }

        private void sourceFilePathTextBox_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string folderPath = dialog.SelectedPath;
                sourceFilePathTextBox.Text = folderPath;
            }
        }
    }
}
